package com.dsbj.apolo.facecollector.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsbj.apolo.facecollector.entity.Employee;
import com.dsbj.apolo.facecollector.entity.Persont;
import com.dsbj.apolo.facecollector.entity.Questionnaire;
import com.dsbj.apolo.facecollector.service.EmployeeService;
import com.dsbj.apolo.facecollector.service.PersontService;
import com.dsbj.apolo.facecollector.service.QuestionnaireService;
import com.dsbj.apolo.facecollector.utils.strprocess.RespStruct;
import com.dsbj.apolo.facecollector.utils.strprocess.StrUtil;
import org.apache.catalina.ssi.ExpressionParseTree;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cjw 2020/2/4
 */
@Controller
@RequestMapping(value = "/questionnaire")
public class QuestionnaireController {
	
	@Resource
	private QuestionnaireService questionnaireService;
	
	@Resource
	private PersontService persontService;
	
	@Resource
	private EmployeeService employeeService;
	
	
	
	@RequestMapping("/gotoQuestionnaire")
	public String gotoQuestionnaire(){
		return "questionnaire/add";
	}
	
	
	/**
	 * 存入表单信息。
	 * @param questionnaire
	 * @return
	 */
	@RequestMapping(value = "/addQuestionnaire")
	@ResponseBody
	public RespStruct addQuestionnaire(Questionnaire questionnaire,
									   String residenceAddressDes, String whereNowDes,
									   String feverTemperature,String durationDay,String seeDoctor,String diagnosticResult){
		
		StrUtil.prettyPrint("questionnaire", questionnaire);
		
		/**
		 * 验证传来的参数
		 */
		RespStruct respStruct=new RespStruct();
		//限制一个用户提交一次。
		RespStruct respStruct_temp=sumitOnlyOneCheck(questionnaire);
		if(respStruct_temp.getCode()==0){
			return respStruct_temp;
		}
		//验证用户信息是否正确
		respStruct_temp=checkQuestionnaire(questionnaire);
		if(respStruct_temp.getCode()==0){
			return respStruct_temp;
		}
		
		/**
		 * 拼接字段值。
		 */
		//居住地址
		String residenceAddress=questionnaire.getResidenceAddress();
		if("其他地址".equals(residenceAddress)){
			residenceAddress=residenceAddress+"--"+residenceAddressDes;
			questionnaire.setResidenceAddress(residenceAddress);
		}
		//当前所在城市
		String whereNow=questionnaire.getWhereNow();
		if("外地".equals(whereNow)){
			whereNow=whereNow+"--"+whereNowDes;
			questionnaire.setWhereNow(whereNow);
		}
		
		//健康状况详情（"发热/咳嗽/感冒"）
		String physicalCondition=questionnaire.getPhysicalCondition();
		if("发热/咳嗽/感冒".equals(physicalCondition)){
			String physicalConditionDes=null;
			
			physicalConditionDes="发热温度: "+feverTemperature+"; 持续天数: "+durationDay
					+"; 是否就医: "+seeDoctor+"; 诊断结果: "+diagnosticResult;
			questionnaire.setPhysicalConditionDes(physicalConditionDes);
		}
		
		/**
		 * 表单提交时，默认将所有字段都提交了，此处将一些字段置为null。
		 */
		//春节是否去过苏州以外城市
		if(questionnaire.getBeenToOtherplace()==0){
			questionnaire.setDestination(null);
			questionnaire.setGoOffTime(null);
			questionnaire.setGoOffTransport(null);
			questionnaire.setGoOffTransportDes(null);
			
			questionnaire.setReturnTime(null);
			questionnaire.setReturnTransport(null);
			questionnaire.setReturnTransportDes(null);
		}
		
		//是否去过湖北或温州
		if(questionnaire.getBeenToGivencity()==0){
			questionnaire.setGivencity(null);
			questionnaire.setReturnback(null);
			questionnaire.setReturnbackTime(null);
			questionnaire.setReturnbackTransport(null);
			questionnaire.setReturnbackTransportDes(null);
		}
		
		//是否与湖北省出来人员接触过
		if(questionnaire.getContactPersonFromGivencity()==0){
			questionnaire.setContactTime(null);
			questionnaire.setContactWay(null);
			questionnaire.setContactWayDes(null);
		}
		
		//现在人在苏州还是在外地
		if(!questionnaire.getWhereNow().contains("外地")){
			questionnaire.setReturnbackPlanTime(null);
			questionnaire.setReturnbackPlanTransport(null);
			questionnaire.setReturnbackPlanTransportDes(null);
		}
		
		//您现在的身体状况
		if("正常".equals(questionnaire.getPhysicalCondition())){
			questionnaire.setPhysicalConditionDes(null);
		}
		
		//身边是否有疑似肺炎人员
		if(questionnaire.getPneumoniaAround()==0){
			questionnaire.setPneumoniaRelation(null);
		}
		
		//是否因外地返回而处于自我隔离
		if(questionnaire.getSelfIsolate()==0){
			questionnaire.setIsolateLocation(null);
			questionnaire.setIsolateStartTime(null);
			questionnaire.setIsolateEndTime(null);
		}
		
		
		//存入调查问卷信息
		questionnaire.setCreateTime(new Date());
		questionnaireService.save(questionnaire);
		
		return respStruct.success();
	}
	
	/**
	 * 校验idcardno是否有效。若有效，给出工号和姓名信息; 若无效，给出提示。
	 *
	 * @params
	 * @return 
	 * @author cjw 2020/2/4
	 */
	@RequestMapping(value = "/checkIdcardno")
	@ResponseBody
	public RespStruct checkIdcardno(String companyno,String idcardno){
		RespStruct respStruct=new RespStruct();
		
		/**
		 * 从两张表中查询出该用户。
		 */
		//从H_P_PERSONT表中查询
		QueryWrapper<Persont> queryWrapper=new QueryWrapper<Persont>()
				.eq("COMPANYNO",companyno)
				.eq("IDCARDNO",idcardno)
				.eq("STATUS",1);
		Persont persont=persontService.getOne(queryWrapper);
		
		//若persont为null
		Employee employee=null;
		if(persont==null){
			QueryWrapper<Employee> queryWrapper_employee=new QueryWrapper<Employee>()
					.eq("COMPANYNO",companyno)
					.eq("IDCARDNO",idcardno)
					.isNull("DISMISSIONDATE")
					.eq("DELETESTATUS",1);
			employee= employeeService.getOne(queryWrapper_employee);
			if(employee==null){
				return respStruct.error("未找到该该身份证号对应的员工信息!");
			}
		}
		String employeeno_server= persont==null? employee.getEmployeeno(): persont.getEmployeeno();
		String employeename_server=persont==null? employee.getEmployeename(): persont.getEmployeename();
		
		Map<String,Object> mapData=new LinkedHashMap<>();
		mapData.put("employeeno", employeeno_server);
		mapData.put("employeename", employeename_server);
		return respStruct.success(mapData);
	}
	
	
	
	/**
	 * 验证传入的调查问卷的有效性。
	 * @param
	 * @return
	 * @author cjw 2020-02-04
	 */
	public RespStruct checkQuestionnaire(Questionnaire questionnaire){
		RespStruct respStruct=new RespStruct();
		
		if(questionnaire==null){
			return respStruct.error("questionnaire为null");
		}
		
		String companyno=questionnaire.getCompanyno();
		String idcardno=questionnaire.getIdcardno();
		String employeeno=questionnaire.getEmployeeno();
		String employeename=questionnaire.getEmployeename();
		
		/**
		 * 从两张表中查询出该用户,并验证用户信息是否正确。
		 */
		//从H_P_PERSONT表中查询
		QueryWrapper<Persont> queryWrapper=new QueryWrapper<Persont>()
				.eq("COMPANYNO",companyno)
				.eq("IDCARDNO",idcardno)
				.eq("STATUS",1);
		Persont persont=persontService.getOne(queryWrapper);
		
		//若persont为null
		Employee employee=null;
		if(persont==null){
			QueryWrapper<Employee> queryWrapper_employee=new QueryWrapper<Employee>()
					.eq("COMPANYNO",companyno)
					.eq("IDCARDNO",idcardno)
					.isNull("DISMISSIONDATE")
					.eq("DELETESTATUS",1);
			employee= employeeService.getOne(queryWrapper_employee);
			if(employee==null){
				return respStruct.error("未找到该身份证对应的员工信息!");
			}
		}
		
		String employeeno_server= persont==null? employee.getEmployeeno(): persont.getEmployeeno();
		//String employeename_server=persont==null? employee.getEmployeename(): persont.getEmployeename();
		//boolean isValid=employeeno_server.equals(employeeno)&&employeename_server.equals(employeename);
		boolean isValid=employeeno_server.equals(employeeno);
		if(isValid==false){
			return respStruct.error("您输入的工号有误!");
		}
		return respStruct.success();
	}
	
	/**
	 * 调查问卷，一个用户只能提交一次。此次检查是否已提交过。
	 * @param questionnaire
	 * @return
	 */
	public RespStruct sumitOnlyOneCheck(Questionnaire questionnaire){
		RespStruct respStruct=new RespStruct();
		if(questionnaire==null){
			return respStruct.error("questionnaire为null!");
		}
		
		String companyno=questionnaire.getCompanyno();
		String idcardno=questionnaire.getIdcardno();
		
		QueryWrapper<Questionnaire> queryWrapper=new QueryWrapper<Questionnaire>()
				.eq("COMPANYNO", companyno)
				.eq("IDCARDNO", idcardno);
		List<Questionnaire> list_questionnaire=questionnaireService.list(queryWrapper);
		//若list_questionnaire中含有信息
		if(list_questionnaire.size()>0){
			return respStruct.error("您已提交过该调查问卷");
		}
		return respStruct.success();
	}
	
	
	
	
}
